import unittest
import requests
from api.login import LoginAPI
from parameterized import parameterized
import json


def build_data():
    test_data = []
    json_file = '../data/login.json'
    with open(json_file, encoding='utf8') as f:
        json_data = json.load(f)
        for i in json_data:
            username = i.get('username')
            password = i.get('password')
            verify_code = i.get('verify_code')
            content_type = i.get('content_type')
            status_code = i.get('status_code')
            status = i.get('status')
            msg = i.get('msg')

            test_data.append((username, password, verify_code, content_type, status_code, status, msg))
        return test_data


class TestLoginAPI(unittest.TestCase):

    def setUp(self):
        self.login_api = LoginAPI()
        self.session = requests.session()

    def tearDown(self):
        if self.session:
            self.session.close()

    @parameterized.expand(build_data())
    def test01_login(self, username, password, verify_code, content_type, status_code, status, msg):
        res = self.login_api.get_verify_code(self.session)
        self.assertEqual(status_code, res.status_code)
        self.assertIn(content_type, res.headers.get("Content-Type"))

        res2 = self.login_api.login(self.session, username, password, verify_code)
        self.assertEqual(status_code, res2.status_code)
        self.assertEqual(status, res2.json().get('status'))
        self.assertIn(msg, res2.json().get('msg'))
